<template>
	<view style="position: relative;">

		<view class="row-between" :style="[{ 'margin-top':+ statusBar+30 + 'px'}]">
			<view class="name">
				CHANGE
			</view>
			<image src="@/static/images/login_service.png" style="margin-right: 40rpx;width: 100rpx;height: 100rpx;">
			</image>
		</view>
		<view class="info">
			<view class="tip">你好，亲爱的用户！</view>
			<input class="input" placeholder="请输入手机号" style="	margin-top: 48px;" v-model="phone" type="number"
				maxlength="11" />
			<view class="row-between" style="	margin-top: 20px;" @click="send">
				<input class="input" style="width: 53%;" placeholder="请输入验证码" v-model="code" />
				<view class="code">{{countdown!=60 ?(countdown+'S'):'获取验证码'}}</view>
			</view>
			<view class="row-h-center" style="margin-top: 15px;">
				<image :src="isArgree?'/static/images/radio_y.png':'/static/images/radio_n.png' " class="radio"
					@click="clickAgree()"></image>
				<view class="agreement">阅读并同意《用户协议》和《隐私协议》</view>
			</view>

			<view class="bottom">
				<view class="btn" @click="goLogin">登&nbsp;&nbsp;录</view>
				<view class="tourist" @click="goIndex">以游客身份浏览</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login,
		sendCode,
		getInfo
	} from '@/api/change/index.js'

	export default {
		data() {
			return {
				codeUrl: "",
				captchaEnabled: true,
				globalConfig: getApp().globalData.config,
				statusBar: 0,
				phone: '',
				code: '',
				isArgree: false,
				timer: null,
				countdown:60,
			}
		},
		onLoad() {
			this.getStatucBarHeight()
		},

		onUnload() {
			this.clearTimer()
		},

		methods: {
			goIndex(){
				uni.reLaunch({
					url:'/pages/index'
				})
			},
			clickAgree() {
				this.isArgree = !this.isArgree
			},
			loading() {
				//启动定时器
				this.countdown--; //定时器减1
			},
			clearTimer() {
				//清除定时器
				clearInterval(this.timer);
				this.timer = null;
			},
			send() {
				if(this.countdown!=60){
					return
				}
				if (this.phone.length != 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				sendCode({
					phone: this.phone
				}).then(res => {
					if (res.code == 0) {
						this.timer = setInterval(() => {
							if (this.countdown === 1) {
								this.clearTimer(); //关闭定时器
								this.count = 60
							} else {
								this.loading();
							}
						}, 1000);
					}
				})
			},

			goLogin() {
				if (this.phone.length != 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				if (!this.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return
				}
				if (!this.isArgree) {
					uni.showToast({
						title: '请同意协议',
						icon: 'none'
					})
					return
				}
				login({
					phone: this.phone,
					code: this.code
				}).then(res => {
				
					if(res.code == 0){
					uni.setStorageSync('token',res.token)
					uni.setStorageSync('uid',res.userId)
					
						console.log('tokenssss',uni.getStorageSync('token'))
					
					uni.reLaunch({
						url:'/pages/index'
					})
					}else{
					}
				})
			},

	

			goIndex() {
				console.log('goIndexgoIndex')
				uni.reLaunch({
					url: '/pages/index'
				})
			},
			getStatucBarHeight() {
				uni.getSystemInfo({
					success: (e) => {
						this.statusBar = e.statusBarHeight
					}
				})
			},

			// 隐私协议
			handlePrivacy() {
				let site = this.globalConfig.appInfo.agreements[0]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.img
						this.loginForm.uuid = res.uuid
					}
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入您的密码")
				} else if (this.loginForm.code === "" && this.captchaEnabled) {
					this.$modal.msgError("请输入验证码")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				}).catch(() => {
					if (this.captchaEnabled) {
						this.getCode()
					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index')
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: url("~@/static/images/login_bg.png") no-repeat;
		background-size: 100% 100%;
	}

	.name {
		margin-left: 40rpx;
		font-size: 96rpx;
		color: white;
		font-family: HappyZcool-2016;
	}

	.info {
		display: inline;
		flex-direction: column;
		position: relative;
		padding: 31px 25px 29px 25px;
		width: calc(100% - 40px);
		position: fixed;
		bottom: 40px;
		margin-left: 20px;
		margin-right: 20px;
		height: 500px;
		line-height: 20px;
		border-radius: 15px;
		background-color: #93D2F3;
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: center;
		font-family: Arial;
		border: 3px solid rgba(79, 79, 79, 1);
	}

	.tip {
		text-align: left;
		font-family: HappyZcool-2016;
		font-size: 14px;
		color: #101010;
	}

	.input {
		padding-left: 10px;
		background-color: white;
		height: 50px;
		border-radius: 10px;
		color: rgba(136, 136, 136, 1);
		font-size: 14px;
		text-align: left;
		font-family: Microsoft Yahei;
		border: 3px solid rgba(79, 79, 79, 1);
	}

	.code {
		width: 40%;
		height: 50px;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 10px;
		background-color: #FE794A;
		color: #4F4F4F;
		font-size: 14px;
		text-align: center;
		font-family: HappyZcool-2016;
		border: 3px solid rgba(79, 79, 79, 1);
	}

	.agreement {
		margin-left: 10px;
		color: white;
		font-size: 14px;
		text-align: left;
		font-family: HappyZcool-2016;
	}

	.radio {
		width: 15px;
		height: 15px;
	}

	.bottom {
		bottom: 69px;
		position: fixed;
		display: flex;
		flex-direction: column;
		width: calc(100% - 90px);
		line-height: 20px;
		border-radius: 25px;
		background-color: rgba(255, 255, 255, 1);
		color: rgba(16, 16, 16, 1);
		font-size: 14px;
		text-align: center;
		font-family: Arial;
		border: 3px solid rgba(79, 79, 79, 1);
	}



	.btn {
		margin-top: 35px;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 70px;
		border-radius: 15px;
		background-color: rgba(255, 191, 107, 1);
		color: #101010;
		font-size: 28px;
		margin-left: 30px;
		margin-right: 30px;
		text-align: center;
		font-family: HappyZcool-2016;
		border: 3px solid rgba(79, 79, 79, 1);
	}

	.tourist {
		margin-top: 25px;
		color: #347CAF;
		font-size: 20px;
		font-family: HappyZcool-2016;
		margin-bottom: 20px;
	}
</style>